<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        function calculate(str) {
            if (arguments.length !== 1) {
                console.log("参数不对");
            }
            let flag = false, sum = 0, temp = 0;
            var strarr = str.split("");
            for (let i = 0, j = strarr.length; i != j; i++) {
                if (strarr[i] == 'O') {
                    if (flag === false) {
                        flag = true;  
                    } 
                    temp ++;
                    sum += temp;
                } else if (strarr[i] == 'X') {
                    if (flag === true) {
                        flag = false;
                        temp = 0;
                    }
                } else {
                    throw new Error('错误的字符');
                }
            }
            console.log(sum);
        }
        calculate("OOXXOXXOOO");
    </script>
</body>

</html>